﻿using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Yuebon.Commons.Encrypt;
using Yuebon.Commons.Json;

namespace Yuebon.Commons.Helpers
{
    /// <summary>
    /// 获取电脑硬件信息
    /// </summary>
    public class HardHelper
    {
        /// <summary>
        /// 获取CPU信息
        /// </summary>
        /// <returns></returns>
        public static string GetCpuInfo()
        {
            var info = new List<string>();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // Windows 平台使用 WMI
                using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
                foreach (var item in searcher.Get())
                {
                    info.Add($"CPU: {item["Name"]}");
                    info.Add($"核心数: {item["NumberOfCores"]}");
                    info.Add($"逻辑处理器: {item["NumberOfLogicalProcessors"]}");
                }
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // Linux 平台读取 /proc/cpuinfo
                try
                {
                    var lines = System.IO.File.ReadAllLines("/proc/cpuinfo");
                    foreach (var line in lines)
                    {
                        if (line.StartsWith("model name"))
                        {
                            info.Add($"CPU: {line.Split(':')[1].Trim()}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    
                }
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                // macOS 平台使用 sysctl
                try
                {
                    var psi = new System.Diagnostics.ProcessStartInfo
                    {
                        FileName = "sysctl",
                        Arguments = "-n machdep.cpu.brand_string",
                        RedirectStandardOutput = true,
                        UseShellExecute = false
                    };

                    using var process = System.Diagnostics.Process.Start(psi);
                    if (process != null)
                    {
                        process.WaitForExit();
                        info.Add($"CPU: {process.StandardOutput.ReadToEnd().Trim()}");
                    }
                }
                catch (Exception ex)
                {
                    
                }
            }
            else
            {
                
            }
            if (info.Count > 0) {
                return Cryptography.GetSHA512(JsonConvert.SerializeObject(info));
            }
            else
            {
                return "";
            }
            
        }
    
    }
}
